# Maintainer: Balló György <ballogyor+arch at gmail dot com>
# Contributor: Vitaliy Berdinskikh ur6lad[at]i.ua

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - (make)depend on java-runtime and -environmennt >=8 (fails to build with 7)
#  - patch from debian to fix build on ARM

pkgname=java-rxtx
_pkgname=rxtx
pkgver=2.2pre2
pkgrel=10
pkgdesc="Java library for serial IO"
arch=('x86_64')
url="http://rxtx.qbang.org/"
license=('LGPL')
depends=('glibc' 'java-runtime>=8' 'systemd')
makedepends=('java-environment>=8')
provides=("$_pkgname=$pkgver")
replaces=("$_pkgname")
install=$pkgname.install
source=(http://rxtx.qbang.org/pub/$_pkgname/$_pkgname-$pkgver.zip
        utsrelease.patch
        rxtx-2.2-lock.patch
        rxtx-2.2-fhs_lock.patch
        rxtx-2.2-format-security.patch
        ttyACM_port.patch
        java10.patch
        java11.patch
        rxtx-2.2-undefined_symbol.patch
        rxtx-sys_io_h_check.patch)
md5sums=('7eedb18e3f33a427e2b0e9be8ce3f94c'
         '2f21ec5eb108f871815242698b6150f1'
         '1f7c43d582bfe9daea22d7f7057436da'
         'f4d22d263f45cd1d4db6242dd0ac78ae'
         '32094214422b717a31841146ca78400d'
         '903a3fe0067d0682dd5f64483c741df6'
         '683dd95e6e419b2b63851c08ede7ca86'
         '1db5c64e239c80294d00c932237889dd'
         '4695fe9bb28a7c9b21447f998fb46b02'
         '6fa4482df20625f652ecad818e92614a')
sha256sums=('3c30373e760f444def3650c76c5a00ae12fb1d860ec008750d084f4880495b03'
            '307ddf4bdcd5ba8d71a10d54253d9b6c3b531f5c6e2619b6895701d2cb81325a'
            '93d76b9747e3cf9a7d2e8767a4e3e977afa0ff83277e17d85727a423f68faa18'
            'c77d2e0a85dd3eb6edbe16fb7fd58a07d55356ce6b4e77b26cf1f38a72aa13c1'
            '6c00f319f64b490e99b2bfca93f6b353dfc2a99f5915fd48af3a46d76b08ab0f'
            'f67db773131805d5344972e32f79cc9272b4dde6a44733ba4edb298107d36c55'
            '21108e0dd258b4b7d4d0abf22207a3bf34e9df8d78bd9909d1810df00ea5a359'
            '3d9729cbdb2de9e41869bb355b2e6d6f7b4f32386219ef1a8694c72dc856cbf0'
            'bb289c83b66cc314a3b3c9ddc20c342577d76accdd49d2e2b24921a02689725d')

prepare() {
  cd $_pkgname-$pkgver

  # Fix build
  patch -Np1 -i ../utsrelease.patch

  # Fix lockdir patch
  patch -Np1 -i ../rxtx-2.2-lock.patch

  # Fix buffer overflow
  patch -Np1 -i ../rxtx-2.2-fhs_lock.patch

  # Enable more ports
  patch -Np1 -i ../ttyACM_port.patch

  # Fix build with openjdk10
  patch -Np1 -i ../java10.patch

  # Fix build with openjdk11
  patch -Np1 -i ../java11.patch
  
  # Fix undefined symbol
  patch -Np1 -i ../rxtx-2.2-undefined_symbol.patch

  # Fix format-security errors
  patch -p1 -i ../rxtx-2.2-format-security.patch

  # Fix ARM build
  patch -Np1 -i ../rxtx-sys_io_h_check.patch

  rm *.m4
  autoreconf -fi
}

build() {
  cd $_pkgname-$pkgver
  ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var
  make -j1
}

package() {
  cd $_pkgname-$pkgver
  mkdir -p "$pkgdir"/usr/{lib,share/java/rxtx}
  make JHOME="$pkgdir/usr/share/java/rxtx" RXTX_PATH="$pkgdir/usr/lib" install

  # Setup lock directory
  install -dm755 "$pkgdir"/usr/lib/tmpfiles.d
  echo 'd /run/lock/lockdev 0775 root lock -' > "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
}
